#include <iostream>
#include <thread>
#include <queue>
#include <mutex>
#include "TaskQueue.h"

bool TaskQueue::empty()
{
    std::unique_lock<std::mutex> locker(queue_mutex);
    return task_queue.empty();
}

size_t TaskQueue::size()
{
    std::unique_lock<std::mutex> locker(queue_mutex);
    return task_queue.size();
}

void TaskQueue::push_queue(int task)
{
    std::unique_lock<std::mutex> push_lock(queue_mutex);
    task_queue.emplace(task);
    std::cout << "存入一个TaskID: " << task << "的任务" << std::endl;
    std::cout << std::endl;
    condition.notify_one();
}

void TaskQueue::pop_queue(int task)
{
    std::unique_lock<std::mutex> pop_lock(queue_mutex);
    condition.wait(pop_lock, [this] {
        if (task_queue.empty())
            return false;
        return true;
        });
    task = std::move(task_queue.front());
    task_queue.pop();
    pop_lock.unlock();
    std::cout << "取出一个TaskID: " << task << "的任务" << std::endl;
    startTask(task);
    std::cout << std::endl;
}

void TaskQueue::startTask(int task) {
    std::cout << "开始执行任务" << "TaskID: " << task << std::endl;
}

